package com.situ.web.service.impl;

import com.situ.web.dao.IUserDao;
import com.situ.web.dao.impl.UserDaoImpl;
import com.situ.web.pojo.entity.Banji;
import com.situ.web.pojo.entity.User;
import com.situ.web.pojo.query.UserQuery;
import com.situ.web.service.IUserService;
import com.situ.web.util.LayUITableResult;
import com.situ.web.util.PageInfo;

import java.util.List;

public class UserServiceImpl implements IUserService {
    private IUserDao userDao = new UserDaoImpl();

    @Override
    public PageInfo selectByPage(Integer pageNo, Integer pageSize) {
        //Service业务成为了完成PageInfo封装，需要调用两次dao操作
        //1.查找当前页数据
        int offset = (pageNo - 1) * pageSize;
        List<User> list = userDao.selectByPage(offset, pageSize);
        //2.总的页数 查总的数量 Math.ceil(总数量/pageSize)
        int totalCount = userDao.selectTotalCount();
        int totalPage = (int) Math.ceil((double) totalCount / pageSize);

        PageInfo<User> pageInfo = new PageInfo<User>(list, totalPage, pageNo, pageSize);
        return pageInfo;
    }

    @Override
    public User login(String name, String password){
        return userDao.login(name,password);
    }

    @Override
    public LayUITableResult selectByPage(UserQuery userQuery) {
        List<User> list = userDao.selectByPage(userQuery);

        int totalCount = userDao.selectTotalCount(userQuery);

        return  LayUITableResult.ok(totalCount, list);
    }
}
